package com.xgkj.diyiketang.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;

import com.easemob.easeui.domain.EaseUser;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.hxsdk.DemoDBManager;
import com.xgkj.diyiketang.hxsdk.DemoHelper;
import com.xgkj.diyiketang.task.GetUserInfoTask;
import com.xgkj.diyiketang.util.PalLog;

/**
 * Created by windo on 2016/1/7.
 */
public class NewMessageReceive extends BroadcastReceiver {

    public static final String NEWMESSAGE_ACTION = "new_message_receive_action";
    public static final String NEWMESSAGE_REFRESH_ACTION = "new_message_refresh_action";
    private int getUserInfoTaskId = -1;
    private Context mContext;
    private static final String TAG = "接受消息广播";

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        PalLog.e(TAG,"开始");
        if(intent != null && intent.getAction().equals(NEWMESSAGE_ACTION)){
            final String from = intent.getStringExtra("from");

            if(from != null){
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Looper.prepare();
                        doGetUserInfo(mContext, from);
                        Looper.loop();
                    }
                });
                thread.start();
            }
        }
    }

    private void doGetUserInfo(Context context, final String from){
        GetUserInfoTask task = new GetUserInfoTask(from);
        task.addListener(new ShowToastTaskEventListener(context){
            @Override
            public void handleTaskCompleted(int taskId, int result, Object data) {
                super.handleTaskCompleted(taskId, result, data);
                if(data != null){
                    EatShowUserInfo info = (EatShowUserInfo) data;
                    if (info != null){
                        String nick = info.getNickName();
                        String userLogo = info.getUserLogo();
                        EaseUser user = new EaseUser(from);
                        user.setNick(nick);
                        user.setAvatar(userLogo);
                        EaseUser cacheUser = DemoDBManager.getInstance().getUser(from);
                        if(cacheUser != null && (!nick.equals(cacheUser.getNick()) || !userLogo.equals(cacheUser.getAvatar()))) {
                            DemoHelper.getInstance().saveContact(user);
                            PalLog.e(TAG, "保存用户成功");
                            Intent intent = new Intent(NEWMESSAGE_REFRESH_ACTION);
                            mContext.sendBroadcast(intent);
                        }else{
                            PalLog.e(TAG,"用户已存在");
                        }
                    }
                }
            }

            @Override
            public void handleTaskError(int taskId, int errorCode, String errorMsg) {
                super.handleTaskError(taskId, errorCode, errorMsg);
            }
        });
        getUserInfoTaskId = EatShowApp.getInstance().getTaskManager().addTask(task);
    }
}
